package com.tbit.uqbike.service.business.brand.config;

import com.tbit.uqbike.constant.BrandRemoteConfigConstant;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.BrandRemoteConfig;
import com.tbit.uqbike.webmanager.util.StringUtils;
import org.springframework.stereotype.Component;

/**
 * 免登录配置校验
 *
 *
 * @author <a href="mailto:chen.chaoxiong@tbit.com.cn">ChenChaoXiong</a>
 * @since 2024/5/9
 */
@Component
public class NoLoginValidator extends AbstractConfigValidator{
    @Override
    protected boolean support(BrandRemoteConfig brandRemoteConfig) {
        return BrandRemoteConfigConstant.NO_LOGIN_EXPIRY_DAYS.equals(brandRemoteConfig.getConfigType());
    }

    @Override
    public void doValid(BrandRemoteConfig brandRemoteConfig) {
        String value = brandRemoteConfig.getParamKV();
        if (StringUtils.isBlank(value)) {
            throw new BaseException("免登录有效期不能为空");
        }

        try {
            int intValue = Integer.parseInt(value);
            //分钟
            if (intValue < 1 || intValue > 525600) {
                throw new BaseException("免登录有效期不能超过一年(525600分钟)");
            }
        } catch (NumberFormatException e) {
            throw new BaseException("免登录有效期必须是整数");
        }
    }
}
